<template>
  <!--  分配他人弹框  -->
  <a-modal
    v-model:visible="state.showAllocationModal"
    title="选择分配人员"
    :footer="null"
    @cancel="closeModal"
    :destroy-on-close="true"
    width="30vw"
  >
    <a-form :form="state.tableParams" layout="inline">
      <a-form-item label="分配人员：">
        <a-spin :spinning="state.allocationModalSpinning">
          <a-select
            v-model:value="state.assignedID"
            show-search
            option-filter-prop="children"
            placeholder="请选择"
            style="width: 15vw"
          >
            <a-select-option
              v-for="item in state.allocationOptions"
              :key="item.userId"
            >
              {{ item.fullName }}
            </a-select-option>
          </a-select>
        </a-spin>
      </a-form-item>
    </a-form>
    <div class="text-center mt20">
      <a-space>
        <a-button
          type="primary"
          @click="confirmAllocation"
          :loading="
            state.allocationModalSpinning || state.loadingConfirmAllocationBtn
          "
        >
          确定
        </a-button>
        <a-button @click="closeModal"> 取消 </a-button>
      </a-space>
    </div>
  </a-modal>
</template>
<script setup>
import {
  todo_get_config_user_post,
  todo_handover_post
} from 'server/modules/7537'
import { reactive, toRef, watch } from 'vue'
import { message } from 'ant-design-vue'
const state = reactive({
  showAllocationModal: false,
  tableParams: null,
  allocationModalSpinning: false,
  assignedID: null,
  allocationOptions: null,
  loadingConfirmAllocationBtn: false
})

state.showAllocationModal = toRef(props, 'showAllocationModal')

const props = defineProps(['showAllocationModal', 'record'])

const emit = defineEmits(['closeModal', 'refresh'])

watch(
  () => props.showAllocationModal,
  val => {
    if (val) {
      state.allocationModalSpinning = true
      // TODO 根据  业务代码 + 类型（如：核价/调整工艺，确定由哪个方法实际执行）
      todoGetConfigUser(props.record)
      // TODO 初始化弹窗select框默认选中人
    }
  }
)
// 获取转移人员数组
async function todoGetConfigUser(task) {
  const params = {}
  // 核价员时
  if (task.businessType === 2) {
    params.businessModelCode = task.businessModelCode
    params.userDuty = 'valuer'
  }

  const { success, retInfo, data } = await todo_get_config_user_post(params)
  state.allocationModalSpinning = false
  if (success) {
    state.allocationOptions = data
    for (const item of data) {
      if (task.receiver === item.userId) {
        state.assignedID = item.userId
        break
      }
    }
  } else {
    console.log(retInfo)
  }
}
async function confirmAllocation() {
  state.loadingConfirmAllocationBtn = true

  // // TODO 調用分配接口
  const { success, retInfo, data } = await todo_handover_post({
    id: props.record.id,
    receiver: state.assignedID
  })
  state.loadingConfirmAllocationBtn = false
  if (success) {
    message.success(data)
    emit('refresh')
  } else {
    console.log(retInfo)
  }
  closeModal()
}
function closeModal() {
  emit('closeModal')
}
</script>
